$(function(){

    /* 登入注册表单 */
    $('body').append(
        '<div class="success-poup-box" id="activity-form-success">' +         // 成功提示窗
        '    <div class="box-contect1">报名成功</div>' +
        '    <div class="box-contect2">我们会尽快与您联系，谢谢！</div>' +
        '</div>'+
        '<div class="activity-form" id="activity-form">' +                      // 表单
        '    <div class="part-cont">' +
        '        <div class="close-form"></div>' +
        '        <div>' +
        '            <p class="form-main-title">现在填写报名信息</p>' +
        '            <p class="form-sub-title">专属名校菁英1对4课程体验！</p>' +
        '        </div>' +
        '        <form action="" method="post" id="myform">' +
        '            <input type="hidden"  class="input-remark" name="remark" value="PC：免费试听">' +
        '            <input type="text" class="input-item input-name" placeholder="请输入您的姓名" name="name">' +
        '            <div class="select-2-col">' +
        '                <div class="input-wrap">' +
        '                    <select class="select-item" name="grade">' +
        '                    </select>' +
        '                </div>' +
        '                <div class="input-wrap">' +
        '                    <select class="select-item" name="subject">' +
        '                        <option value="数学">数学</option>' +
        '                        <option value="英语">英语</option>' +
        '                        <option value="语文">语文</option>' +
        '                        <option value="物理">物理</option>' +
        '                        <option value="化学">化学</option>' +
        '                        <option value="地理">地理</option>' +
        '                    </select>' +
        '                </div>' +
        '            </div>' +
        '            <input type="text" class="input-item input-phone" placeholder="请输入您的手机号" name="mobile">' +
        '            <div class="form-btn">获得0元试听</div>' +
        '            <div class="form-btn-close"></div>' +
        '        </form>' +
        '    </div>' +
        '</div>'
    );

    form_check();   // 表单送出
    has_form();     // 需用表单
    select_unselected("#activity-form [name=grade]");   // 未选择时项目灰
    select_unselected("#activity-form [name=subject]");


    function has_form(){
        // 如 poupform = yes 则表示，此区块需使用弹出表单
        $('[poupform="yes"]').click(function () {
            var maintitle = $(this).attr('maintitle');
            var subtitle = $(this).attr('subtitle');
            var btn = $(this).attr('btn');
            var remark = $(this).attr('remark');
            var grade = $(this).attr('grade');
            var subject = $(this).attr('subject');

            if(maintitle){
                $('#activity-form .form-main-title').text(maintitle);
            }
            else {
                $('#activity-form .form-main-title').text("现在填写报名信息");
            }
            if(subtitle){
                $('#activity-form .form-sub-title').text(subtitle);
            }
            else{
                $('#activity-form .form-sub-title').text("专属名校菁英1对4课程体验！");
            }
            if(btn){
                $('#activity-form .form-btn').text(btn);
            }
            else{
                $('#activity-form .form-btn').text("获得0元试听");
            }
            if(remark){
                $('#activity-form .input-remark').val(remark);
            }
            else{
                $('#activity-form .input-remark').val("PC：免费试听");

            }
            if(grade){
                select_option("grade",grade);
            }
            else {
                var grade="小学三年级/小学四年级/小学五年级/小学六年级/初一/初二/初三/高一/高二/高三";
                select_option("grade",grade);
            }
            if(subject){
                select_option("subject",subject);
            }
            else{
                var subject="数学/英语/语文/物理/化学/地理";
                select_option("subject",subject);
            }

            // 表单显示
            $("#activity-form").fadeIn();
        });
        $('#activity-form .close-form').click(function () {
            // 表单关闭
            $("#activity-form").fadeOut();
        });
    }

    // 表单下拉项可自行增减
    function select_option(name,data){
        $('#activity-form [name="'+name+'"]').html(''); // 清空成功
        var val=data.split("/");
        var length = val.length;
        for (var i = 0; i < length; i++) {
            $('#activity-form [name="'+name+'"]').append('<option value="'+val[i]+'">'+val[i]+'</option>');
        }
    }

    function form_check(){
        $("#activity-form .form-btn").click(function(){
            var name = $("#activity-form .input-name").val(),
                phone = $("#activity-form .input-phone").val(),
                grade = $("#activity-form select[name='grade']").val(),
                subject = $("#activity-form select[name='subject']").val(),
                remark = $("#activity-form .input-remark").val();
            if(!name){
                alert("姓名不能为空");
                return false;
            }
            if(!phone){
                alert("手机号码不能为空");
                return false;
            }
            if(!(/^1[34578]\d{9}$/.test(phone))){
                alert("手机号码有误");
                return false;
            }
            if(!grade){
                alert("年级不能为空");
                return false;
            }
            if(!subject){
                alert("科目不能为空");
                return false;
            }
            if(!remark){
                alert("备注不能为空");
                return false;
            }
            if(name&&phone&&(/^1[34578]\d{9}$/.test(phone))&&grade&&subject&&remark){
                $(".form-btn-close").fadeIn("slow");

                $("#activity-form .form-btn").text("正在提交......").css("cssText","background-color: rgb(189, 189, 189);");

            }
            var post = {"name":name,"phone":phone,"grade":grade,"subject":subject,"remarke_type":2,'remark':remark};
            $.ajax({
                type: "POST",dataType:"json", url: "index.php?s=mobile", data: post,
                success: function(data) {
                    if (data.status) {
                        // dr_tips('报名成功....', 3, 1);
                        $("#activity-form-success").delay(1000).fadeIn("slow");
                        $("#activity-form-success").delay(2000).fadeOut();
                        $("#activity-form").delay(500).fadeOut(500); //表单
                        $("#activity-form .form-btn-close").delay(500).fadeOut("slow");
                        $("#activity-form .form-btn").text("免费试听").css("cssText","background: #3a7eff;");
                        dr_tips(data.code);
                        setTimeout('window.location.href = data.backurl;', 3000);
                    } else {
                        dr_tips(data.code);
                    }
                },
                error: function(HttpRequest, ajaxOptions, thrownError) {
                    alert(HttpRequest.responseText);
                }
            });
        });

        // 关闭（发送成功）用提醒
        $('.success-poup-box').click(function(e) {
            $(this).fadeOut();
        });
    }

    function select_unselected(select){
        $(select).css('color','#9e9e9e');
        $(select).change(function() {
            var current = $(this).val();
            if (current) {
                $(this).css('color','#333333');
            } else {
                $(this).css('color','#9e9e9e');
            }
        });
    }

});
